﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Csharp结业设计
{
    public partial class quary_delete_student : Form
    {
        public quary_delete_student()
        {
            InitializeComponent();
        }

        //设置连接字符串
        string ConnectionString = "Data Source =LAPTOP-7I3EOIVU\\SQLEXPRESS ; Initial Catalog = Performance_management_system;" + "Integrated Security = true;";
        DataSet dataset = null;     //new DataSet();  错误 //创建数据集     

        private void showdata2()
        {
            //创建一个新连接 
            SqlConnection conn = new SqlConnection(ConnectionString);
            try
            {
                dataset = new DataSet();
                //创建数据提供者
                SqlDataAdapter DataAdapter = new SqlDataAdapter("SELECT * FROM Student_infor", conn);// Where 姓名=
                DataAdapter.Fill(dataset, "Student_infor");
                dataGridView1.DataSource = dataset;
                dataGridView1.DataMember = "Student_infor";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {

                if (conn != null) conn.Close();
                conn.Dispose();
                dataset.Dispose();
            }
        }

        SqlConnection conn = null;
        //SqlDataAdapter DataAdapter = null;
        //DataSet dataset = null;
        string curNo = "";

        private void button1_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count <= 1) return;
            int index = dataGridView1.CurrentRow.Index;//获取当前记录的索引号
            dataGridView1.Rows[index].Selected = true;//加亮显示
            curNo = this.dataGridView1.Rows[index].Cells[0].Value.ToString();
            SqlCommand command = null;
            int x = int.Parse(curNo);
            string strSQL = "Delete From Student_infor Where 学号 = " + x;
            try
            {
                command = new SqlCommand();
                command.Connection = conn;
                command.CommandText = strSQL;
                conn.Open();
                int n = command.ExecuteNonQuery();    //执行Delete语句
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (conn != null) conn.Close();
                command.Dispose();
            }
            showdata2();
        }

        private void quary_delete_student_Load(object sender, EventArgs e)
        {
            conn = new SqlConnection(ConnectionString);
            showdata2();
        }
    }
}
